﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Database;
using Business;
using System.Globalization;

namespace DT5_Anh09110006_Hung09110044_Vu09110157.QuanLy
{
    public partial class SuaNhanVien : System.Web.UI.Page
    {
        protected string GiaTriNgaySinh { get; set; }
        Business.QuanLyNhanVien qlnhanvien = new Business.QuanLyNhanVien();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MaNhanVien"] != null)
                {
                    loaddulieu();
                }
                else
                {
                    Response.Redirect("QuanLyHocVien.aspx");
                }
            }
        }
        private void loaddulieu()
        {
            ddlNguoiQuanLy.DataSource = qlnhanvien.LayDanhSachNhanVien();
            ddlNguoiQuanLy.DataTextField = "HoTenNV";
            ddlNguoiQuanLy.DataValueField = "MaNV";
            ddlNguoiQuanLy.DataBind();
            String manhanvien = Session["MaNhanVien"].ToString();
            NhanVien nv= qlnhanvien.LayNhanVienTheoMa(manhanvien);
            txtMaNV.Text=nv.MaNV;
            txtHoTenNV.Text=nv.HoTenNV;
             this.GiaTriNgaySinh = nv.NgaySinh.Value.ToString("MM/dd/yyyy");
            txtDiaChiNV.Text=nv.DiaChi;
            ddlGioiTinhHV.SelectedValue=nv.GioiTinh;          
            ddlChucVu.SelectedValue=nv.ChucVu;
            txtLương.Text=nv.Luong.ToString();
            txtDienThoaiNV.Text=nv.DienThoai;
            ddlNguoiQuanLy.SelectedValue = nv.MaNQL;
            txtCMNDNV.Text=nv.CMND;
            txtEmailNV.Text = nv.Email;
        }
        protected void btnThemNV_Click(object sender, EventArgs e)
        {
            Session["MaNhanVien"] = null;
            NhanVien nhanvien = new NhanVien
            {
                MaNV = txtMaNV.Text,
                HoTenNV = txtHoTenNV.Text,
                NgaySinh = System.DateTime.ParseExact(Request["txtNgaysinh"], "MM/dd/yyyy", CultureInfo.InvariantCulture),
                DiaChi = txtDiaChiNV.Text,
                GioiTinh = ddlGioiTinhHV.SelectedValue,
                ChucVu = ddlChucVu.SelectedValue,
                Luong = float.Parse(txtLương.Text),
                DienThoai = txtDienThoaiNV.Text,
                CMND = txtCMNDNV.Text,
                Email = txtEmailNV.Text,

            };
            if (qlnhanvien.SuaNhanVien(nhanvien))
            {
                Response.Write("<script language='javascript' type='text/javascript'>"
                                   +" alert('Chỉnh sửa thành công');"
                                   + "window.location='QuanLyNhanVien.aspx'"
                                   +"</script>");
                
            }
            else
            {
                Response.Write("<script language='javascript' type='text/javascript'>"
                                 + " alert('Không thể sửa nhân viên này');"
                                 + "window.location='QuanLyNhanVien.aspx'"
                                 + "</script>");
            }            
        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Session["MaNhanVien"] = null;
            Response.Redirect("QuanLyNhanVien.aspx");
        }
    }
}